package com.ruoyi.common.utils.file;

import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.DefaultFastFileStorageClient;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Component
public class FastDFSUtil {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
//    private DefaultFastFileStorageClient defaultFastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    //注入缩略图
    @Autowired
    private ThumbImageConfig thumbImageConfig;


    /**
     * 上传文件
     * @param file MultipartFile多媒体文件对象
     * @return 文件全路径，不含Http地址部分
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), getExt(file.getOriginalFilename()), null);
        String fullPath = storePath.getFullPath();
        return fullPath;

    }

    /**
     * 上传文件
     * @param file File文件对象
     * @return 文件全路径，不含Http地址部分
     */
    public String uploadFile(File file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), getExt(file.getName()), null);
            return storePath.getFullPath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 上传获得文件缩略图
     * @param file File文件对象
     * @return 缩略图全路径
     */
    public String getThumbImage(File file) {
        try {
            // 上传并且生成缩略图
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(
                    new FileInputStream(file), file.length(), "jpg", null);
            String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
            return path;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 下载文件
     * @param filePath 文件路径
     * @return
     */
    public byte[] downloadFile(String filePath) {
        StorePath storePath = StorePath.praseFromUrl(filePath);
        byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
        return bytes;
    }

    /**
     * 删除文件
     * @param filePath 文件路径
     * @return
     */
    public Boolean deleteFile(String filePath) {
        if (StringUtils.isEmpty(filePath)) {
            return false;
        }
        try {
            StorePath storePath = StorePath.praseFromUrl(filePath);
            fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    // 封装文件完整URL地址
    public String getResAccessUrl(String path) {
        String url = fdfsWebServer.getWebServerUrl() + path;
        System.out.println("上传文件地址为：\n" + url);
        return url;
    }
    //获得文件扩展名
    private String getExt(String fileName) {
        //扩展名
        String ext = FilenameUtils.getExtension(fileName);
        if(StringUtils.isEmpty(ext)) {
            System.out.println("===源文件名===" + fileName);
            //扩展名
            ext = fileName.substring(fileName.lastIndexOf(".") + 1);
            if(StringUtils.isEmpty(ext))
                ext = "jpg";
        }
        System.out.println("===扩展名===" + ext);
        return ext;
    }
}
